package com.flyduck.modules.system.service.impl;

import com.flyduck.modules.system.domain.SysUser;
import com.flyduck.modules.system.domain.SysUserJob;
import com.flyduck.modules.system.domain.SysUserRole;
import com.flyduck.modules.system.mapper.SysUserJobMapper;
import com.flyduck.modules.system.mapper.SysUserMapper;
import com.flyduck.modules.system.mapper.SysUserRoleMapper;
import com.flyduck.modules.system.service.SysUserService;
import com.flyduck.common.vo.PageResult;
import com.flyduck.common.vo.QueryVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: flyduck-admin
 * @description:
 * @author: flyduck
 * @create: 2020-05-06 13:23
 **/
@Service
public class SysUserServiceImpl extends BaseServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserJobMapper sysUserJobMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public PageResult findPage(QueryVo queryVo) {
        //分页查询
        PageHelper.startPage(queryVo.getPage(), queryVo.getRows());
        Page<SysUser> sysUserPage = sysUserMapper.findPage(queryVo.getKeyword());
        //分页结果集
        PageResult pageResult = new PageResult();
        pageResult.setTotal(sysUserPage.getTotal());
        pageResult.setRows(sysUserPage.getResult());
        return pageResult;
    }

    /**
     * 保存用户
     * @param sysUser
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysUser sysUser) {
        //md5加密密码
        Md5Hash md5Hash = new Md5Hash(sysUser.getPassword(), sysUser.getUsername(), 2);
        sysUser.setPassword(md5Hash.toString());

        //保存用户
        //=====保存用户初始化
        sysUser.setCreateName(getUser().getUsername());
        sysUser.setCreateTime(new Date());
        sysUserMapper.insertSelective(sysUser);
        //事务测试
        int i = 1/0;
        //保存用户职位关系
        Long[] jobIds = sysUser.getJobIds();
        if (jobIds != null) {
            for (Long jobId : jobIds) {
                SysUserJob sysUsersJobs = new SysUserJob();
                sysUsersJobs.setJobId(jobId);
                sysUsersJobs.setUserId(sysUser.getId());
                sysUserJobMapper.insertSelective(sysUsersJobs);
            }
        }
        //保存用户角色关系
        Long[] roleIds = sysUser.getRoleIds();
        if (roleIds != null) {
            for (Long roleId : roleIds) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUser.getId());
                sysUserRole.setRoleId(roleId);
                sysUserRoleMapper.insertSelective(sysUserRole);

            }
        }

    }

    /**
     * 根据用户id  启用/禁用
     * @param id
     * @param enabled
     */
    @Override
    public void updateStatus(Long id, Boolean enabled) {
        sysUserMapper.updateStatus(id, enabled);
    }

    /**
     * 根据用户id查询用户
     * @return
     */
    @Override
    public SysUser getUserById(Long id) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(id);
        //查询用户的角色
        List<SysUserRole> sysUsersRoles = sysUserRoleMapper.findByUserId(sysUser.getId());
        List<Long> roleIds = sysUsersRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        sysUser.setRoleIds(roleIds.toArray(new Long[0]));
        //查询用户的职位
        List<SysUserJob> sysUsersJobs = sysUserJobMapper.findByUserId(sysUser.getId());
        List<Long> jobIds = sysUsersJobs.stream().map(SysUserJob::getJobId).collect(Collectors.toList());
        sysUser.setJobIds(jobIds.toArray(new Long[0]));
        return sysUser;

    }

    @Override
    public void update(SysUser sysUser) {
        //更新用户
        //=====保存用户初始化
        sysUser.setUpdateName(getUser().getUsername());
        sysUser.setUpdateTime(new Date());
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
        //更新用户角色关系
        //======1.解除之前的关系
        sysUserRoleMapper.deleteByUserId(sysUser.getId());
        //======2.建立新关系
        Long[] roleIds = sysUser.getRoleIds();
        for (Long roleId : roleIds) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUser.getId());
            sysUserRole.setRoleId(roleId);
            sysUserRoleMapper.insertSelective(sysUserRole);

        }

        //更新用户职位关系
        //======1.解除之前的关系
        sysUserJobMapper.deleteByUserId(sysUser.getId());
        //======2.建立新关系
        Long[] jobIds = sysUser.getJobIds();
        for (Long jobId : jobIds) {
            SysUserJob sysUserJob = new SysUserJob();
            sysUserJob.setJobId(jobId);
            sysUserJob.setUserId(sysUser.getId());
            sysUserJobMapper.insertSelective(sysUserJob);
        }

    }

    /**
     * 根据用户名从数据库查用户信息
     * @param username
     * @return
     */
    @Override
    public SysUser findUserByUsername(String username) {
       return sysUserMapper.findUserByUsername(username);
    }


}
